package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;


    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //1.保存套餐基本信息
        this.save(setmealDto);
        //2.保存套餐菜品关联信息
        Long id = setmealDto.getId();   //获取当前套餐的id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();    //获取套餐菜品关联数据
        setmealDishes.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (ids) and status=1
        //查询当前删除的套餐状态是否可以删除
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId, ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0){
            //如果要删除的数组中有启售状态的不能删除 给他抛出一个自定义异常
            throw new CustomException("套餐正在售卖中 不能被删除");
        }
        //如果可以删除 先删除套餐表中的数据
        this.removeByIds(ids);
        //delete from setmeal where setmeal_id in (ids)
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(dishLambdaQueryWrapper);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //1.先查基本信息
        Setmeal setmeal = this.getById(id);
        //2.根据套餐的id查询套餐的菜品信息
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        //获取当前套餐的菜品信息
        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);
        //3.整合套餐信息
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        //4.返回套餐dto
        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //1.修改套餐的基本信息
        this.updateById(setmealDto);
        //2.修改菜品信息 先把原来的菜品信息全部删掉 再添加新的菜品信息
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        //凡是满足当前查询条件的数据全部删除
        setmealDishService.remove(lambdaQueryWrapper);
        //添加提交过来的菜品信息
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();    //获取所有的菜品信息
        dishes = dishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }
}
